# -*- coding: utf-8 -*-
import re
from django import forms
from django.contrib.auth.models import User
from models import Profile


class LoginForm(forms.Form):

    username = forms.CharField(
        label=u'用户名:',
        max_length=30
    )
    password = forms.CharField(
        label=u'密码:',
        widget=forms.PasswordInput()
    )

    def clean_username(self):
        username = self.cleaned_data['username']
        if not re.search(r'^\w+$', username):
            raise forms.ValidationError(u'用户名只能包含字母数字字符和下划线.')
        else:
            return username

    def clean_password(self):
        password = self.cleaned_data['password']
        if 5 < len(password) < 20:
            return password
        else:
            raise forms.ValidationError(u'密码至少需要六个字符.')


class RegistrationForm(forms.Form):

    username = forms.CharField(
        label=u'用户名',
        max_length=30
    )
    email = forms.EmailField(label=u'Email')
    password = forms.CharField(
        label=u'密码',
        widget=forms.PasswordInput()
    )
    confirm = forms.CharField(
        label=u'确认密码',
        widget=forms.PasswordInput()
    )

    def clean_username(self):
        username = self.cleaned_data['username']
        if not re.search(r'^\w+$', username):
            raise forms.ValidationError(u'用户名只能包含字母数字字符和下划线')
        try:
            User.objects.get(username=username)
        except User.DoesNotExist:
            return username
        raise forms.ValidationError(u'该用户已经存在')

    def clean_email(self):
            email = self.cleaned_data['email']
            try:
                User.objects.get(email=email)
            except User.DoesNotExist:
                return email
            raise forms.ValidationError(u'邮箱已被注册.')

    def clean_confirm(self):
        if 'password' in self.cleaned_data:
            password = self.cleaned_data['password']
            confirm = self.cleaned_data['confirm']
            if 5 < len(password) < 20:
                if password == confirm:
                    return confirm
                else:
                    raise forms.ValidationError(u'两次输入密码不一样')
            else:
                raise forms.ValidationError(u'密码至少需要六个字符.')


class SettingForm(forms.Form):

    avatar = forms.ImageField(label=u'头像', required=False)

    password = forms.CharField(
        label=u'密码',
        widget=forms.PasswordInput()
    )

    nickname = forms.CharField(
        label=u'昵称',
        max_length=30
    )

    homepage = forms.URLField(label=u'主页', required=False)
    about = forms.CharField(
        label=u'关于',
        max_length=1000,
        widget=forms.Textarea(),
        initial=u'请输入个人简介'
    )


    # def clean_nickname(self):
    #     nickname = self.cleaned_data['nickname']
    #     if not re.search(r'^\w+$', nickname):
    #         raise forms.ValidationError(u'昵称只能包含字母数字字符和下划线.')
    #     else:
    #         return nickname


class PostForm(forms.Form):

    message = forms.CharField(
        label=u'',
        max_length=140,
        widget=forms.Textarea(),
        initial=u'发送一条Tweet'
    )

class RTForm(forms.Form):

    rtmessage = forms.CharField(
        label=u'',
        max_length=140,
        widget=forms.Textarea(),
    )

class ReplyForm(forms.Form):

    replymessage = forms.CharField(
        label=u'',
        max_length=140,
        widget=forms.Textarea(),
    )


class PrivateMessageForm(forms.Form):

    privatemsg = forms.CharField(
        label=u'',
        max_length=1000,
        widget=forms.Textarea(),
        initial=u'这里写私信...'
    )
